package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.geom.Rectangle;
	import flash.net.URLVariables;
	import flash.utils.getTimer;
	
	import flashx.textLayout.formats.BackgroundColor;

	[SWF(width="760", height="600")] //can set in project?
	public class FBGame extends Sprite
	{
		protected var backBuffer:BitmapData;
		protected var Width:int = 760;
		protected var Height:int = 600;
		static public var stateStack:StateStack;
		protected var tBegin:int = 0;
		protected var myGraphics:MyGraphics;
		static public var mainFBGame:FBGame;
		static public var debug:Boolean = false;
		static public var UserID:String = "1";
		static public var charPanelVisible:Boolean = false;
		public function FBGame()
		{
			mainFBGame = this;
			stateStack = new StateStack();
			//stateStack.Push(new StateDrawLoadedImage());
			myGraphics = new MyGraphics(Width, Height);
			addEventListener(Event.ENTER_FRAME,Render);
			//stage.addEventListener(MouseEvent.CLICK,MouseClick);
			stage.addEventListener(MouseEvent.MOUSE_MOVE,MouseMove);
			stage.addEventListener(MouseEvent.MOUSE_DOWN,MouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP,MouseUp);
			stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);
			stateStack.Push(new StateFirst());
		}
		protected function KeyDown(event:KeyboardEvent):void
		{
			if(stateStack.GetCurrent() != null)
			{
				stateStack.GetCurrent().KeyDown(event.keyCode);
			}
		}
		protected function Render(event:Event):void
		{
			//var rc:Rectangle = new Rectangle(0,0,Width,Height);
			//backBuffer.fillRect(rc,0xFF888888);
			//static 
			if(stateStack.GetCurrent() != null)
			{
				var tNew:int = flash.utils.getTimer();
				stateStack.GetCurrent().Update(tNew - tBegin);
				tBegin = tNew;
				stateStack.GetCurrent().Render(myGraphics);
			}
			myGraphics.Flip(this.graphics);
		}
		/*protected function MouseClick(eventObject:MouseEvent):void
		{
			if(stateStack.GetCurrent() != null)
				stateStack.GetCurrent().MouseClick(eventObject.localX,eventObject.localY);
		}
		*/
		protected function MouseMove(eventObject:MouseEvent):void
		{
			if(stateStack.GetCurrent() != null)
				stateStack.GetCurrent().MouseMove(eventObject.localX,eventObject.localY, eventObject.buttonDown);
		}
		protected function MouseDown(eventObject:MouseEvent):void
		{
			if(stateStack.GetCurrent() != null)
				stateStack.GetCurrent().MouseDown(eventObject.localX,eventObject.localY);
		}
		protected function MouseUp(eventObject:MouseEvent):void
		{
			if(stateStack.GetCurrent() != null)
				stateStack.GetCurrent().MouseUp(eventObject.localX,eventObject.localY);
		}
		public function ReceiveMessage(mess:String,param:Array,sender:MovieClip):void
		{
			if(stateStack.GetCurrent() != null)
				stateStack.GetCurrent().ReceiveMessage(mess,param,sender);
		}
	}
}